<template>
  <div class="body" v-loading="loading">
    <div>
      <el-button type="info" size="small" icon="el-icon-back" @click="gotoList">&nbsp;返&nbsp;回&nbsp;</el-button>
      <el-button type="primary" size="small" icon="el-icon-tickets" @click="toTPALOrder" v-if="header.status === 'audit'">生成盘盈亏</el-button>
      <el-button type="warning" size="small" icon="el-icon-s-promotion" @click="doUnAudit" v-if="header.status === 'audit'">&nbsp;反&nbsp;审&nbsp;</el-button>
      <el-button type="primary" size="small" icon="el-icon-s-promotion" @click="doAudit" v-else :disabled="!header.billNo">&nbsp;审&nbsp;核&nbsp;</el-button>
      <el-button type="primary" size="small" icon="el-icon-s-promotion" @click="toExport" >导出盘点作业</el-button>
    </div>
    <el-form ref="form" :model="header" size="small" label-width="80px" :inline="true" class="header">
      <div class="audit_icon iconfont" v-if="header.status === 'audit'"></div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="作业单号">{{ header.billNo }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="方案单号">{{ header.stockTakeNo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="方案名称">{{ header.stockTakeName }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="仓库">{{ getStockName(header.stockFrom) }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库">{{ getStockName(header.stockTo) }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-switch size="mini" v-model="header.stockTakeMethod" disabled active-text="盲盘" inactive-text="明盘"></el-switch>
        </el-col>
        <el-col :span="6">
          <span style="font-size: 14px">复盘</span>
          <el-switch style="padding-left: 5px" size="mini" v-model="header.review" disabled active-color="#13ce66" inactive-color="#DCDFE6"></el-switch>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="物料">{{ header.materialFrom }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料">{{ header.materialTo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-checkbox v-model="header.addMaterial" disabled>物料盘点允许增加物料</el-checkbox>
        </el-col>
        <el-col :span="6">
          <el-form-item v-if="!header.stockTakeMethod" label="全部箱数">{{ header.sumBoxCount }}</el-form-item>
          <el-form-item v-if="!header.stockTakeMethod" label="已扫箱数">{{ header.sweptCount }}</el-form-item>
        </el-col>
      </el-row>
      <br />
    </el-form>
    <el-table :data="dataList" class="detail" height="30vh">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="仓库" min-width="120" prop="stockNo">
        <template #default="scope">
          {{ scope.row.stockNo }}:{{ getStockName(scope.row.stockNo) }}
        </template>
      </el-table-column>
      <el-table-column label="物料" min-width="300" prop="materialNo">
        <template #default="scope">
          {{ scope.row.materialNo }}:{{ scope.row.materialName }} {{ scope.row.materialSpec }}
        </template>
      </el-table-column>
      <el-table-column label="单位" width="80" prop="unitName"></el-table-column>
      <el-table-column label="批次" min-width="140" prop="batchNo"></el-table-column>
      <el-table-column label="账面库存" width="80" prop="bookCount" align="right">
        <template #default="scope">
          {{ scope.row.bookCount? Number(scope.row.bookCount.toFixed(6)): scope.row.bookCount }}
        </template>
      </el-table-column>
      <el-table-column label="账面箱数" width="80" prop="bookBoxCount" align="right"></el-table-column>
      <el-table-column label="盘点库存" width="150" prop="count" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')">{{ scope.row.count? Number(scope.row.count.toFixed(6)): "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盘点箱数" width="150" prop="boxCount" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')">{{ scope.row.boxCount || "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="header.review" label="复盘库存" width="150" prop="reviewCount" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')">{{ scope.row.reviewCount? Number(scope.row.reviewCount.toFixed(6)): "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="header.review" label="复盘箱数" width="150" prop="reviewBoxCount" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')">{{ scope.row.reviewBoxCount || "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盘盈亏数" width="150" prop="TPALCount" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')" :class="!scope.row.TPALCount? '':(scope.row.TPALCount > 0? 'plus': 'minus')">{{ scope.row.TPALCount? Number(scope.row.TPALCount.toFixed(6)): scope.row.TPALCount }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盘盈亏箱数" width="150" prop="TPALBoxCount" align="right">
        <template #default="scope">
          <div v-if="(scope.row.status === 'save' || scope.row.review === 'save')" :class="!scope.row.TPALBoxCount? '':(scope.row.TPALBoxCount > 0? 'plus': 'minus')">{{ scope.row.TPALBoxCount }}</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="复盘盈亏数" width="150" prop="reviewTPALCount" align="right">-->
<!--        <template #default="scope">-->
<!--          <div v-if="scope.row.status === 'save'" :class="!scope.row.reviewTPALCount? '':(scope.row.reviewTPALCount > 0? 'plus': 'minus')">{{ scope.row.reviewTPALCount? Number(scope.row.reviewTPALCount.toFixed(6)): scope.row.reviewTPALCount }}</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="复盘盈亏箱数" width="150" prop="reviewTPALBoxCount" align="right">-->
<!--        <template #default="scope">-->
<!--          <div v-if="scope.row.status === 'save'" :class="!scope.row.reviewTPALBoxCount? '':(scope.row.reviewTPALBoxCount > 0? 'plus': 'minus')">{{ scope.row.reviewTPALBoxCount }}</div>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="操作" width="160" fixed="right">
        <template #default="scope">
          <el-button size="medium" type="text" style="color: #409EFF" icon="el-icon-mouse" @click="getBox(scope.row)">查看箱号</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table :data="detailBoxList" class="detail" height="calc(100vh - 550px)">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="箱号" width="200" prop="boxNo"></el-table-column>
      <el-table-column label="仓位" width="150" prop="locationNo"></el-table-column>
      <el-table-column label="重量" width="120" align="right" prop="bookCount"></el-table-column>
      <el-table-column label="盘点库存" width="120" align="right" prop="count">
        <template #default="scope">
          <div v-if="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')">{{ scope.row.count || 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="复盘库存" width="120" align="right" prop="reviewCount">
        <template #default="scope">
          <div v-if="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')">{{ scope.row.reviewCount || 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盘盈亏数" width="120" align="right" prop="TPALCount">
        <template #default="scope">
          <div v-if="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')" :class="!scope.row.TPALCount? '':(scope.row.TPALCount > 0? 'plus': 'minus')">{{ scope.row.TPALCount? Number(scope.row.TPALCount.toFixed(6)): "-" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="120"  prop="status">
        <template #default="scope">
          <el-tag :type="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')? 'success': 'warning'">{{ (scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review') ? '已盘': '未盘' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="盘点人员" width="120"  prop="userName">
        <template #default="scope">
          <div v-if="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')">{{ scope.row.userName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="复盘人员" width="120"  prop="userName">
        <template #default="scope">
          <div v-if="(scope.row.detailStatus === 'save' || scope.row.inventoryStatus === 'review')">{{ scope.row.reviewUserName }}</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getBoxNo } from '../../utils'

export default {
  data() {
    return {
      loading: false,
      materialLoading: false,
      materialList: [],
      supplierLoading: false,
      supplierList: [],
      header: {},
      dataList: [],
      detailBoxList: [],
      selection: [],
      stockList: [],
    }
  },
  async mounted() {
    await this.getStock()
    let billNo = this.$route.query.billNo
    if (billNo) {
      return this.getBill(billNo)
    }
  },
  methods: {
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    async toExport() {
      this.loading = true
      try {
        let res = await this.callApi('download_stock_take_task_detail', {billNo : this.header.billNo})
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoList() {
      return this.$router.push('/stockTake/stockTakeTask')
    },
    async getBill(billNo) {
      this.loading = true
      try {
        let res = await this.callApi('get_stock_take_task_detail', {
          billNo: billNo,
        })
        if (res.res) {
          this.header = res.header || {}
          this.dataList = res.data || []
        }
      } finally {
        this.loading = false
      }
    },
    async getBox(data) {
      let key = data.key
      let billNo = data.billNo
      this.loading = true
      try {
        let res = await this.callApi('get_stock_take_task_detail_box', {
          key,
          billNo
        })
        if (res.res) {
          this.detailBoxList = res.data || []
          this.changeDetailCount()
        }
      } finally {
        this.loading = false
      }
    },
    async doAudit() {
      this.loading = true
      try {
        let res = await this.callApi('audit_stock_take_task', { billNo: this.header.billNo })
        if (!res.res) {
          return
        }
        this.reload()
        this.header.status = "audit"
        this.$message.success('审核完成!')
      } finally {
        this.loading = false
      }
    },
    async doUnAudit() {
      this.loading = true
      try {
        let res = await this.callApi('unaudit_stock_take_task', { billNo: this.header.billNo })
        if (!res.res) {
          return
        }
        this.header.status = "save"
        this.$message.success('反审完成!')
      } finally {
        this.loading = false
      }
    },
    async toTPALOrder() {
      try {
        await this.$confirm(`确定生成盘盈亏单吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('to_TPAL_order', { billNo: this.header.billNo })
        if (!res.res) {
          return
        }
        let message = res.message
        this.$message.success(message)
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.body {
  padding: 20px 0;
  .header {
    margin-top: 20px;
    position: relative;
    .header_row {
      display: flex;
      >div {
        flex-shrink: 0;
      }
      :deep(.produceDate) {
        .el-input {
          width: 100%!important;
        }
      }
    }
    .audit_icon {
      position: absolute;
      top: -10px;
      left: 60px;
      font-size: 48px;
      color: red;
    }
    .material {
      width: 100%;
    }
    .p3 {
      :deep(.el-form-item__content) {
        display: flex;
        .comment {
          margin-left: 10px;
          word-break: keep-all;
        }
      }
    }
  }
  .detail_header {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    .stat {
      display: flex;
      font-size: 15px;
      >div {
        display: flex;
        align-items: baseline;
        line-height: 32px;
        .label {
          text-align: right;
          margin-right: 5px;
          color: #aaa;
        }
        .value {
          min-width: 50px;
          margin-right: 10px;
          color: #666;
        }
      }
    }
  }
  .detail {
    .plus {
      color: var(--el-color-primary);
    }
    .minus {
      color: var(--el-color-danger);
    }
  }
}
</style>
